package com.zenith.middleware.service.impl;

import com.zenith.common.result.PageResult;
import com.zenith.common.result.Result;
import com.zenith.common.constant.RedisKeyConstant;
import com.zenith.common.constant.RedisTtlConstant;
import com.zenith.middleware.feign.user.UserServiceFeignClient;
import com.zenith.middleware.po.dto.user.PermissionDTO;
import com.zenith.middleware.po.dto.user.RoleDTO;
import com.zenith.middleware.po.dto.user.RolePermissionDTO;
import com.zenith.middleware.po.dto.user.UserDTO;
import com.zenith.middleware.po.dto.user.UserRoleDTO;
import com.zenith.middleware.po.vo.user.PermissionVO;
import com.zenith.middleware.po.vo.user.RoleVO;
import com.zenith.middleware.po.vo.user.UserVO;
import com.zenith.middleware.po.vo.user.RolePermissionVO;
import com.zenith.middleware.po.vo.user.UserRoleVO;
import com.zenith.middleware.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 用户服务实现类
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    
    private final UserServiceFeignClient userServiceFeignClient;
    
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    
    public UserServiceImpl(UserServiceFeignClient userServiceFeignClient) {
        this.userServiceFeignClient = userServiceFeignClient;
    }
    
    /**
     * 检查用户是否是活跃状态
     */
    @Override
    public Result<Boolean> getUserActive(Long userId) {
        return userServiceFeignClient.getUserActive(userId);
    }

    /**
     * 获取当前用户信息接口
     */
    @Override
    public Result<Object> getCurrentUser(Long userId) {
        return userServiceFeignClient.getCurrentUser(userId);
    }

    // =========================== 用户管理相关接口 ===========================
    
    /**
     * 创建用户
     */
    @Override
    public Result<String> createUser(UserDTO userDTO) {
        Result<String> result = userServiceFeignClient.createUser(userDTO);
        // 清除相关缓存
        if (result != null) {
            clearUserCache();
        }
        return result;
    }
    
    /**
     * 更新用户
     */
    @Override
    public Result<String> updateUser(UserDTO userDTO) {
        Result<String> result = userServiceFeignClient.updateUser(userDTO);
        // 清除相关缓存
        if (result != null) {
            clearUserCache();
        }
        return result;
    }
    
    /**
     * 删除用户
     */
    @Override
    public Result<String> deleteUser(Long id) {
        Result<String> result = userServiceFeignClient.deleteUser(id);
        // 清除相关缓存
        if (result != null) {
            clearUserCache();
        }
        return result;
    }
    
    /**
     * 获取用户详情
     */
    @Override
    public Result<UserVO> getUserById(Long id) {
        // 构建缓存键
        String cacheKey = RedisKeyConstant.USER_DETAIL + id;
        
        // 尝试从缓存获取
        Result<UserVO> cachedResult = (Result<UserVO>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取用户详情数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<UserVO> result = userServiceFeignClient.getUserById(id);
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.USER_INFO_TTL, TimeUnit.SECONDS);
            log.info("缓存用户详情数据: key={}", cacheKey);
        }
        
        return result;
    }
    
    /**
     * 按用户名查询
     */
    @Override
    public Result<UserVO> getUserByUsername(String username) {
        // 构建缓存键
        String cacheKey = "userByUsername:" + username;
        
        // 尝试从缓存获取
        Result<UserVO> cachedResult = (Result<UserVO>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取用户信息数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<UserVO> result = userServiceFeignClient.getUserByUsername(username);
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.USER_INFO_TTL, TimeUnit.SECONDS);
            log.info("缓存用户信息数据: key={}", cacheKey);
        }
        
        return result;
    }
    
    /**
     * 分页查询用户
     */
    @Override
    public Result<PageResult<UserVO>> getUserPage(UserDTO query) {
        // 构建缓存键
        String cacheKey = RedisKeyConstant.USER_PAGE + (query.getPageNum() != null ? query.getPageNum() : 1) + "_" + 
                         (query.getPageSize() != null ? query.getPageSize() : 10);
        
        // 尝试从缓存获取
        Result<PageResult<UserVO>> cachedResult = (Result<PageResult<UserVO>>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取用户分页数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<PageResult<UserVO>> result = userServiceFeignClient.getUserPage(query);
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.USER_PAGE_TTL, TimeUnit.SECONDS);
            log.info("缓存用户分页数据: key={}", cacheKey);
        }
        
        return result;
    }
    
    /**
     * 批量查询用户
     */
    @Override
    public Result<List<UserVO>> getUsersByIds(List<Long> ids) {
        // 构建缓存键
        String cacheKey = "usersByIds:" + ids.toString();
        
        // 尝试从缓存获取
        Result<List<UserVO>> cachedResult = (Result<List<UserVO>>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取批量用户数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<List<UserVO>> result = userServiceFeignClient.getUsersByIds(ids);
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.USER_INFO_TTL, TimeUnit.SECONDS);
            log.info("缓存批量用户数据: key={}", cacheKey);
        }
        
        return result;
    }
    
    /**
     * 更新用户状态
     */
    @Override
    public Result<String> updateUserStatus(Long id, Integer status) {
        Result<String> result = userServiceFeignClient.updateUserStatus(id, status);
        // 清除相关缓存
        if (result != null) {
            clearUserCache();
        }
        return result;
    }
    
    /**
     * 重置密码
     */
    @Override
    public Result<String> resetPassword(Long id, String newPassword) {
        Result<String> result = userServiceFeignClient.resetPassword(id, newPassword);
        // 清除相关缓存
        if (result != null) {
            clearUserCache();
        }
        return result;
    }
    
    /**
     * 获取当前用户信息（新接口）
     */
    @Override
    public Result<UserVO> getCurrentUserInfo(Long userId) {
        // 构建缓存键
        String cacheKey = "currentUser:" + userId;
        
        // 尝试从缓存获取
        Result<UserVO> cachedResult = (Result<UserVO>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取当前用户信息数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<UserVO> result = userServiceFeignClient.getCurrentUserInfo(userId);
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.USER_INFO_TTL, TimeUnit.SECONDS);
            log.info("缓存当前用户信息数据: key={}", cacheKey);
        }
        
        return result;
    }
    
    /**
     * 更新个人资料
     */
    @Override
    public Result<String> updateProfile(UserDTO userDTO) {
        Result<String> result = userServiceFeignClient.updateProfile(userDTO);
        // 清除相关缓存
        if (result != null) {
            clearUserCache();
        }
        return result;
    }
    
    /**
     * 修改密码
     */
    @Override
    public Result<String> changePassword(Long userId, String oldPassword, String newPassword) {
        Result<String> result = userServiceFeignClient.changePassword(userId, oldPassword, newPassword);
        // 清除相关缓存
        if (result != null) {
            clearUserCache();
        }
        return result;
    }
    
    /**
     * 上传头像
     */
    @Override
    public Result<String> uploadAvatar(Long userId, String avatarUrl) {
        Result<String> result = userServiceFeignClient.uploadAvatar(userId, avatarUrl);
        // 清除相关缓存
        if (result != null) {
            clearUserCache();
        }
        return result;
    }
    
    /**
     * 批量分配用户角色
     */
    @Override
    public Result<String> batchAssignRoles(List<Long> userIds, List<String> roleIds) {
        Result<String> result = userServiceFeignClient.batchAssignRoles(userIds, roleIds);
        // 清除相关缓存
        if (result != null) {
            clearUserCache();
        }
        return result;
    }
    
    /**
     * 批量删除用户（旧接口）
     */
    @Override
    public Result<Void> deleteUsers(List<String> ids) {
        Result<Void> result = userServiceFeignClient.deleteUsers(ids);
        // 清除相关缓存
        if (result != null) {
            clearUserCache();
        }
        return result;
    }
    
    /**
     * 高级搜索用户
     */
    @Override
    public Result<PageResult<UserVO>> searchUsers(String keyword, Integer status, String role, Integer pageNum, Integer pageSize) {
        // 构建缓存键
        String cacheKey = "searchUsers:" + keyword + "_" + status + "_" + role + "_" + 
                         (pageNum != null ? pageNum : 1) + "_" + (pageSize != null ? pageSize : 10);
        
        // 尝试从缓存获取
        Result<PageResult<UserVO>> cachedResult = (Result<PageResult<UserVO>>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取搜索用户数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<PageResult<UserVO>> result = userServiceFeignClient.searchUsers(keyword, status, role, pageNum, pageSize);
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.USER_PAGE_TTL, TimeUnit.SECONDS);
            log.info("缓存搜索用户数据: key={}", cacheKey);
        }
        
        return result;
    }

    // =========================== 权限管理相关接口 ===========================
    
    @Override
    public Result<PermissionVO> getPermissionById(String id) {
        // 构建缓存键
        String cacheKey = "permissionById:" + id;
        
        // 尝试从缓存获取
        Result<PermissionVO> cachedResult = (Result<PermissionVO>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取权限详情数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<PermissionVO> result = userServiceFeignClient.getPermissionById(id);
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.DEFAULT_TTL, TimeUnit.SECONDS);
            log.info("缓存权限详情数据: key={}", cacheKey);
        }
        
        return result;
    }

    @Override
    public Result<List<PermissionVO>> getPermissionList() {
        // 构建缓存键
        String cacheKey = "permissionList";
        
        // 尝试从缓存获取
        Result<List<PermissionVO>> cachedResult = (Result<List<PermissionVO>>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取权限列表数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<List<PermissionVO>> result = userServiceFeignClient.getPermissionList();
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.DEFAULT_TTL, TimeUnit.SECONDS);
            log.info("缓存权限列表数据: key={}", cacheKey);
        }
        
        return result;
    }

    @Override
    public Result<PageResult<PermissionVO>> getPermissionPage(PermissionDTO query) {
        // 构建缓存键
        String cacheKey = "permissionPage:" + (query.getPageNum() != null ? query.getPageNum() : 1) + "_" + 
                         (query.getPageSize() != null ? query.getPageSize() : 10);
        
        // 尝试从缓存获取
        Result<PageResult<PermissionVO>> cachedResult = (Result<PageResult<PermissionVO>>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取权限分页数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<PageResult<PermissionVO>> result = userServiceFeignClient.getPermissionPage(query);
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.DEFAULT_TTL, TimeUnit.SECONDS);
            log.info("缓存权限分页数据: key={}", cacheKey);
        }
        
        return result;
    }

    @Override
    public Result<Boolean> getPermissionIds(Long userId, String requiredPermission) {
        return userServiceFeignClient.getPermissionIds(userId, requiredPermission);
    }

    @Override
    public Result<List<PermissionVO>> selectPermissionsByRole(String role) {
        // 构建缓存键
        String cacheKey = "permissionsByRole:" + role;
        
        // 尝试从缓存获取
        Result<List<PermissionVO>> cachedResult = (Result<List<PermissionVO>>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取角色权限数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<List<PermissionVO>> result = userServiceFeignClient.selectPermissionsByRole(role);
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.DEFAULT_TTL, TimeUnit.SECONDS);
            log.info("缓存角色权限数据: key={}", cacheKey);
        }
        
        return result;
    }

    // =========================== 角色管理相关接口 ===========================
    
    @Override
    public Result<RoleVO> getRoleById(String id) {
        // 构建缓存键
        String cacheKey = "roleById:" + id;
        
        // 尝试从缓存获取
        Result<RoleVO> cachedResult = (Result<RoleVO>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取角色详情数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<RoleVO> result = userServiceFeignClient.getRoleById(id);
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.DEFAULT_TTL, TimeUnit.SECONDS);
            log.info("缓存角色详情数据: key={}", cacheKey);
        }
        
        return result;
    }

    @Override
    public Result<List<RoleVO>> getRoleList() {
        // 构建缓存键
        String cacheKey = "roleList";
        
        // 尝试从缓存获取
        Result<List<RoleVO>> cachedResult = (Result<List<RoleVO>>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取角色列表数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<List<RoleVO>> result = userServiceFeignClient.getRoleList();
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.DEFAULT_TTL, TimeUnit.SECONDS);
            log.info("缓存角色列表数据: key={}", cacheKey);
        }
        
        return result;
    }

    @Override
    public Result<PageResult<RoleVO>> getRolePage(RoleDTO query) {
        // 构建缓存键
        String cacheKey = "rolePage:" + (query.getPageNum() != null ? query.getPageNum() : 1) + "_" + 
                         (query.getPageSize() != null ? query.getPageSize() : 10);
        
        // 尝试从缓存获取
        Result<PageResult<RoleVO>> cachedResult = (Result<PageResult<RoleVO>>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取角色分页数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<PageResult<RoleVO>> result = userServiceFeignClient.getRolePage(query);
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.DEFAULT_TTL, TimeUnit.SECONDS);
            log.info("缓存角色分页数据: key={}", cacheKey);
        }
        
        return result;
    }

    @Override
    public Result<Void> updateRole(RoleVO entity) {
        Result<Void> result = userServiceFeignClient.updateRole(entity);
        // 清除相关缓存
        if (result != null) {
            clearRoleCache();
        }
        return result;
    }

    // =========================== 角色权限关联管理相关接口 ===========================
    
    @Override
    public Result<RolePermissionVO> getRolePermissionById(String id) {
        // 构建缓存键
        String cacheKey = "rolePermissionById:" + id;
        
        // 尝试从缓存获取
        Result<RolePermissionVO> cachedResult = (Result<RolePermissionVO>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取角色权限关联详情数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<RolePermissionVO> result = userServiceFeignClient.getRolePermissionById(id);
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.DEFAULT_TTL, TimeUnit.SECONDS);
            log.info("缓存角色权限关联详情数据: key={}", cacheKey);
        }
        
        return result;
    }

    @Override
    public Result<List<RolePermissionVO>> getRolePermissionList() {
        // 构建缓存键
        String cacheKey = "rolePermissionList";
        
        // 尝试从缓存获取
        Result<List<RolePermissionVO>> cachedResult = (Result<List<RolePermissionVO>>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取角色权限关联列表数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<List<RolePermissionVO>> result = userServiceFeignClient.getRolePermissionList();
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.DEFAULT_TTL, TimeUnit.SECONDS);
            log.info("缓存角色权限关联列表数据: key={}", cacheKey);
        }
        
        return result;
    }

    @Override
    public Result<PageResult<RolePermissionVO>> getRolePermissionPage(RolePermissionDTO query) {
        // 构建缓存键
        String cacheKey = "rolePermissionPage:" + (query.getPageNum() != null ? query.getPageNum() : 1) + "_" + 
                         (query.getPageSize() != null ? query.getPageSize() : 10);
        
        // 尝试从缓存获取
        Result<PageResult<RolePermissionVO>> cachedResult = (Result<PageResult<RolePermissionVO>>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取角色权限关联分页数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<PageResult<RolePermissionVO>> result = userServiceFeignClient.getRolePermissionPage(query);
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.DEFAULT_TTL, TimeUnit.SECONDS);
            log.info("缓存角色权限关联分页数据: key={}", cacheKey);
        }
        
        return result;
    }

    // =========================== 用户角色关联管理相关接口 ===========================
    
    @Override
    public Result<UserRoleVO> getUserRoleById(String id) {
        // 构建缓存键
        String cacheKey = "userRoleById:" + id;
        
        // 尝试从缓存获取
        Result<UserRoleVO> cachedResult = (Result<UserRoleVO>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取用户角色关联详情数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<UserRoleVO> result = userServiceFeignClient.getUserRoleById(id);
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.DEFAULT_TTL, TimeUnit.SECONDS);
            log.info("缓存用户角色关联详情数据: key={}", cacheKey);
        }
        
        return result;
    }

    @Override
    public Result<List<UserRoleVO>> getUserRoleList() {
        // 构建缓存键
        String cacheKey = "userRoleList";
        
        // 尝试从缓存获取
        Result<List<UserRoleVO>> cachedResult = (Result<List<UserRoleVO>>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取用户角色关联列表数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<List<UserRoleVO>> result = userServiceFeignClient.getUserRoleList();
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.DEFAULT_TTL, TimeUnit.SECONDS);
            log.info("缓存用户角色关联列表数据: key={}", cacheKey);
        }
        
        return result;
    }

    @Override
    public Result<PageResult<UserRoleVO>> getUserRolePage(UserRoleDTO query) {
        // 构建缓存键
        String cacheKey = "userRolePage:" + (query.getPageNum() != null ? query.getPageNum() : 1) + "_" + 
                         (query.getPageSize() != null ? query.getPageSize() : 10);
        
        // 尝试从缓存获取
        Result<PageResult<UserRoleVO>> cachedResult = (Result<PageResult<UserRoleVO>>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取用户角色关联分页数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<PageResult<UserRoleVO>> result = userServiceFeignClient.getUserRolePage(query);
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.DEFAULT_TTL, TimeUnit.SECONDS);
            log.info("缓存用户角色关联分页数据: key={}", cacheKey);
        }
        
        return result;
    }

    @Override
    public Result<Set<String>> getUserRoleIds(Long userId) {
        // 构建缓存键
        String cacheKey = "userRoleIds:" + userId;
        
        // 尝试从缓存获取
        Result<Set<String>> cachedResult = (Result<Set<String>>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取用户角色ID数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<Set<String>> result = userServiceFeignClient.getUserRoleIds(userId);
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.DEFAULT_TTL, TimeUnit.SECONDS);
            log.info("缓存用户角色ID数据: key={}", cacheKey);
        }
        
        return result;
    }

    @Override
    public Result<Set<String>> getUserPermissionIds(Long userId) {
        // 构建缓存键
        String cacheKey = "userPermissionIds:" + userId;
        
        // 尝试从缓存获取
        Result<Set<String>> cachedResult = (Result<Set<String>>) redisTemplate.opsForValue().get(cacheKey);
        if (cachedResult != null) {
            log.info("从缓存获取用户权限ID数据: key={}", cacheKey);
            return cachedResult;
        }
        
        // 调用Feign客户端获取数据
        Result<Set<String>> result = userServiceFeignClient.getUserPermissionIds(userId);
        
        // 缓存结果
        if (result != null && result.getData() != null) {
            redisTemplate.opsForValue().set(cacheKey, result, RedisTtlConstant.DEFAULT_TTL, TimeUnit.SECONDS);
            log.info("缓存用户权限ID数据: key={}", cacheKey);
        }
        
        return result;
    }
    
    // =========================== 缓存清理方法 ===========================
    
    /**
     * 清除用户相关缓存
     */
    private void clearUserCache() {
        redisTemplate.delete(RedisKeyConstant.USER_DETAIL + "*");
        redisTemplate.delete("userByUsername:*");
        redisTemplate.delete(RedisKeyConstant.USER_PAGE + "*");
        redisTemplate.delete("usersByIds:*");
        redisTemplate.delete("currentUser:*");
        redisTemplate.delete("searchUsers:*");
        log.info("清除用户相关缓存");
    }
    
    /**
     * 清除角色相关缓存
     */
    private void clearRoleCache() {
        redisTemplate.delete("roleById:*");
        redisTemplate.delete("roleList");
        redisTemplate.delete("rolePage:*");
        log.info("清除角色相关缓存");
    }
}